# -*- coding:utf-8 -*-
__doc__ = '费用报销首页'

import random
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from PageClass.basePage import BasePage
from Util import logger, ZTCCLOUDError


class EcsIndexPage(BasePage):
    _billBaseXpath = '//div[@class="bill-list block"]/div[@class="content"]/div[@class="menu"]'
    # 事项申请
    _boeApply = (By.XPATH, '//*[@id="app"]/section/main/div/div/div[2]/div[3]/div[2]/div[1]/div/div')
    # 费用报销
    _boeReimburse = (By.XPATH, '//*[@id="app"]/section/main/div/div/div[2]/div[3]/div[2]/div[2]/div/div')
    # 借款还款
    _boeBorrow = (By.XPATH, '//*[@id="app"]/section/main/div/div/div[2]/div[3]/div[2]/div[3]/div/div')
    # 我的发票
    _menuMyInvoice = (By.XPATH, '//*[@id="app"]/section/main/div/div/div[2]/div[3]/div[2]/div[5]/div/div')

    _boeBusinessRefuse = (
        By.XPATH,
        '//*[@id="app"]/section/main/div[2]/div/div/div[3]/div/div[5]/div[2]/table/tbody/tr/td[12]/div/button[2]')
    _boeBusinessTipCancel = (By.XPATH, '/html/body/div[2]/div/div[3]/button[1]')

    def __init__(self, driver):
        BasePage.__init__(self, driver)

    # 打开单据并切换handle
    def open_boe(self, areaType, boeType, operationType=''):
        logger.info(f"待打开的单据类型为：{areaType} 区域中的 {boeType}，选择的业务类型为：{operationType}")
        time.sleep(3)
        areaElementList = self.find_elements(*(By.XPATH, self._billBaseXpath))
        areaFlag = False
        for element in areaElementList:
            if areaType in element.text:
                self._clickElement(element)
                boeAreaList = self.find_elements(*(By.XPATH,
                                                   f'//div[@aria-label="{areaType}"]'
                                                   f'/div[@class="el-dialog__body"]/div'))
                time.sleep(random.random())
                boeFlag = False
                for boeElement in boeAreaList:
                    if boeElement.find_element(*(By.TAG_NAME, 'span')).text == boeType:
                        if not operationType:
                            self._clickElement(boeElement.find_element(*(By.TAG_NAME, 'button')))
                            boeFlag = True
                            break
                        else:
                            operationList = boeElement.find_elements(*(By.XPATH, './div[@class="content"]/div'))
                            operationFlag = False
                            for operationElement in operationList:
                                if operationElement.text == operationType:

                                    self._clickElement(operationElement)
                                    operationFlag = True
                                    boeFlag = True
                                    break
                            if not operationFlag:
                                raise ZTCCLOUDError(f'名称为 {boeType} 的单据区域未找到 {operationType} 业务类型，please check config')
                    if boeFlag:
                        break
                if not boeFlag:
                    raise ZTCCLOUDError(f'{areaType} 区域未找到名称为 {boeType} 的单据，please check config')
                areaFlag = True
            if areaFlag:
                break
        if not areaFlag:
            raise ZTCCLOUDError(f'费用报销微服务首页未找到 {areaType} 区域，please check config')
        self.switchWindow()

    def open_boeApply(self):
        logger.info("打开事项申请单据选择页面")
        self.click(*self._boeApply)

    def open_boeReimburse(self):
        logger.info("打开费用报销单据选择页面")
        self.click(*self._boeReimburse)

    def open_boeBorrow(self):
        logger.info("打开借款还款单据选择页面")
        self.click(*self._boeBorrow)

    def open_menuMyInvoice(self):
        self.click(*self._menuMyInvoice)

    def waitElementLoading(self):
        loc = (By.XPATH, '//div[contains(@class, "el-table__body-wrapper")]//table[1]/tbody/tr')
        s = time.time()
        while 1:
            try:
                WebDriverWait(self.driver, random.random()).until(
                    EC.visibility_of_element_located(loc))
            except Exception as e:
                if time.time() - s > 30:
                    return False
                time.sleep(random.random())
            else:
                return True

    # 我的已审
    _myApproved = (By.XPATH, '//div[@data-url="myApproved"]')

    def click_myApproved(self):
        self.click(*self._myApproved)
        logger.info('点击我的已审')
        self.switchWindow()

    # 待我处理
    _waitDeal = (By.XPATH, '//div[@data-url="waitDeal"]')

    def click_waitDeal(self):
        self.click(*self._waitDeal)
        logger.info('点击待我处理')
        self.switchWindow()

    # 我的单据
    _myBoeList = (By.ID, 'tab-myBoeList')

    def click_myBoeList(self):
        self.click(*self._myBoeList)
        logger.info('点击我的单据')

    # 待我审批
    _myWaitApprove = (By.ID, 'tab-waitApprovel')

    def click_myWaitApprove(self):
        self.click(*self._myWaitApprove)
        logger.info('点击待我审批')

    # 我的草稿
    _userDraft = (By.XPATH, '//div[@data-url="userDraft"]')

    def click_userDraft(self):
        self.click(*self._userDraft)
        logger.info('点击我的草稿')
        self.switchWindow()

    # 更多
    _moreButton = (By.XPATH, '//*[@id="app"]//div[@class="boe-report block"]/div[@class="more-link"]')

    def click_moreButton(self):
        self.click(*self._moreButton)
        logger.info('点击更多按钮')
        self.switchWindow()
